<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
    export default {
        mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
        components: {
            SelectImage: async () => Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectImage())
        },
        data() {
            return {
                appWindow: window,
                form: {
                    name: "",
                    sort: 0,
                    searchStatus: 0,
                    attributeValueLst: []
                },
              
                // 添加时
                value: "",

                // 页面接口地址
                apiUrl: {
                    create: "store/attribute/add",
                    update: "store/attribute/edit",
                    detail: "store/attribute/detail",
                },
            };
        },
        methods: {
            // 添加属性值
            addValue() {
                if(this.value==""){
                    this.$vFN.Tools.messageError("属性值不能为空");
                    return;
                }
                let _is = this.form.attributeValueLst.some(item=>{
                    return (item.attributeValue==this.value)
                })
                if(_is){
                    this.$vFN.Tools.messageError("属性值不能重复");
                    return;
                }
                if (this.id) {
                    this.addValueSubmit()
                } else {
                    this.form.attributeValueLst.push({
                        attributeValue: this.value,
                    })
                    this.value="";
                }
            },
            // 删除属性值
            delValue(item_, index_) {
                this.$confirm(
                        "确定删除  <strong>" + item_.attributeValue + "</strong>  属性值么?",
                        "提示", {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            dangerouslyUseHTMLString: true,
                        }
                    )
                    .then(() => {
                        if (this.id) {
                            this.delValueSubmit(item_.id)
                        } else {
                            this.form.attributeValueLst.splice(index_, 1)
                        }
                    })
                    .catch(() => {});
            },
            // 添加提交
            async addValueSubmit() {
                let _data = await this.$vFN.Http.post({
                    url: "store/attribute/add-value",
                    data: {
                        attributeValue: this.value,
                        attributeId: this.id,
                    },
                }, window);
                if (_data._ok) {
                    this.getValueList();
                    this.value="";
                }
            },
            // 删除提交
            async delValueSubmit(id_) {
                let _data = await this.$vFN.Http.post({
                    url: "store/attribute/del-value",
                    data: {
                        ids: [id_],
                    },
                }, window);
                if (_data._ok) {
                    this.getValueList();
                }
            },
            // 刷新属性值
            async getValueList() {
                let _data = await this.$vFN.Http.post({
                    url: this.apiUrl.detail,
                    data: {
                        id: this.id
                    },
                }, window);
                if (_data._ok) {
                    this.form.attributeValueLst = _data.data.attributeValueLst
                }
            },
            // 初始化
            async init() {
                // 初始化数据
                await this.mixinEdit_editBoxInit({
                    model: "form",
                    id: this.id,
                })
            },


        },
        mounted() {
            this.init();
        },
        activated() {},
    };
</script>